{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python如何快速求得子集list_b在母集list_a的位置(即：开始、终止索引值[2,4])?\n",
    "请教：Python如何快速求得如下子集list_b在母集list_a的位置(即：开始、终止索引值[2,4])，谢谢！\n",
    "\n",
    "list_a=['dd','ss','dd','bp','ss','ef','b1','d5','dd','ss']\n",
    "\n",
    "list_b=['dd','bp','ss']\n",
    "\n",
    "https://www.zhihu.com/question/566256137"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list_a = ['dd','ss','dd','bp','ss','ef','b1','d5','dd','ss']\n",
    "list_b = ['dd','bp','ss']\n",
    "\n",
    "list_a_join = '.'.join(list_a)\n",
    "list_b_join = '.'.join(list_b)\n",
    "idx = list_a_join.find(list_b_join)\n",
    "print(idx)\n",
    "\n",
    "start = len(list_a_join[:idx].split('.'))-1\n",
    "print('[{},{}]'.format(start, start+len(list_b)-1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "python 给定一个十进制整数 N，如何求其对应 2 进制数中 1 的个数？\n",
    "\n",
    "https://www.zhihu.com/question/565886439"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#1011,111111\n",
    "#a = 11\n",
    "a = 63\n",
    "count = 0\n",
    "while a > 0:\n",
    "    if a % 2 == 1:\n",
    "        count +=1\n",
    "    a = a >> 1\n",
    "print(count)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在此python代码中，明明没有对变量x进行运算，为什么变量x会发生变化？\n",
    "https://www.zhihu.com/question/563165302\n",
    "python赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = [1]\n",
    "y = x\n",
    "\n",
    "y[0]=x[0]*2\n",
    "print(y)\n",
    "print(x)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如何使用Python截取16位图像的高8位或者低8位以形成新的8位图像?\n",
    "https://www.zhihu.com/question/565013407"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python两个字典怎么合并并去其中键相同的词条中值较大的?\n",
    "https://www.zhihu.com/question/568423183"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dict_a = {'a': 100, 'b':101, 'c':102}\n",
    "dict_b = {'a': 109, 'd':105}\n",
    "\n",
    "dict_c=dict_a\n",
    "for k, v in dict_b.items():\n",
    "    if k in dict_a.keys():\n",
    "        dict_c[k] = max(v, dict_a[k])\n",
    "    else:\n",
    "        dict_c[k] = v\n",
    "print(dict_c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如何随机创建一个二维数组并保存，文件名为“ch2ex1.csv”，好难，帮帮?\n",
    "1、随机创建一个二维数组并保存为csv；\n",
    "2、读取csv创建ndarray，命名为data，确定数组的维度和数据类型，将其数据类型转换为float32\n",
    "https://www.zhihu.com/question/561680819"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.random.rand(5,5)\n",
    "print(a)\n",
    "#np.savetxt(\"ch2ex1.csv\",a,delimiter=\",\",header='英语，数学',comments=\"\")\n",
    "#np.loadtxt(frame, dtype=np.float, delimiter=None, unpack=False)\n",
    "np.savetxt(\"ch2ex1.csv\",a,delimiter=\",\",comments=\"\")\n",
    "b=np.loadtxt(\"ch2ex1.csv\",dtype=np.float32,delimiter=',')\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#https://www.zhihu.com/question/571874624 浅拷贝与切片\n",
    "#id() 函数返回对象的唯一标识符，标识符是一个整数。\n",
    "#CPython 中 id() 函数用于获取对象的内存地址。\n",
    "#copy模块主要用于可变对象。这样做的目的是复制一个对象，这样就可以在不影响原始对象的情况下对其进行修改。\n",
    "# 因为没有必要复制不可变对象，所以模块拒绝这样做。tuple在Python中是不可变的，\n",
    "import copy\n",
    "vt = (1,2,3,4,5,6,7,8,9,10)\n",
    "vl = [1,2,3,4,5,6,7,8,9,10]\n",
    "\n",
    "print(id(vt), id(vt[1:]))\n",
    "print(id(vl), id(vl[1:]))\n",
    "\n",
    "b = copy.copy(vt)\n",
    "c = copy.copy(vl)\n",
    "print('copy unchangable object tuple ',id(vt),id(b))\n",
    "print('copy changable object list ',id(vl),id(c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "t = torch.tensor([[1, 2], [3, 4]])\n",
    "\n",
    "t0 = t.squeeze(0)\n",
    "\n",
    "t1 = t.squeeze(1)\n",
    "\n",
    "print(t)\n",
    "\n",
    "print(t0)\n",
    "\n",
    "print(t1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.6 ('pylearn')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "2bea6c59ace602b8d5eca7d96ad2dad639c08ee8972ef6d6714d17fddebfe424"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
